package com.san.metadata.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.san.base.controller.BaseController;
import com.san.common.annotation.Log;
import com.san.common.utils.Constant;
import com.san.common.utils.PageUtils;
import com.san.common.utils.Query;
import com.san.common.utils.R;
import com.san.metadata.domain.OperationDO;
import com.san.metadata.service.OperationMetadataService;
import com.san.metadata.vo.OperationVO;

/**
 * 任务管理业务控制类
 */
@Controller
@RequestMapping("/md/operation")
public class OperationMetadataController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(OperationMetadataController.class);
	
	/**
	 * 本功能地址前缀
	 */
	private static final String PREFIX = "metadata/operation";
	
	@Autowired
	private OperationMetadataService operationService;

	@GetMapping("")
	@Log("访问操作元数据主界面")
	@RequiresPermissions("md:operation:view")
	String view() {
		return PREFIX + "/view";
	}

	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<OperationDO> taskScheduleJobList = operationService.list(query);
		int total = operationService.count(query);
		PageUtils pageUtils = new PageUtils(taskScheduleJobList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	@Log("访问任务管理新增页面")
	@RequiresPermissions("md:operation:add")
	String add() {
		return PREFIX + "/add";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@Log("新增操作元")
	@RequiresPermissions("md:operation:add")
	@PostMapping("/save")
	public R save(OperationVO operationVO) {
		try {
			if (Constant.OPERATION_TYPE_CYCLE == operationVO.getOperationType()) {
				operationVO.setStatus(Constant.STATUS_INVALID);
			} else {
				operationVO.setStatus(Constant.STATUS_NORMAL);
			}
			
			if (operationService.save(operationVO) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("任务保存失败",e);
		}
		return R.error();
	}

	@GetMapping("/edit/{id}")
	@Log("访问操作元数据编辑页面")
	@RequiresPermissions("md:operation:edit")
	String edit(@PathVariable("id") Long id, Model model) {
		OperationDO operationDO = operationService.getById(id);
		model.addAttribute("operationDO", operationDO);
		return PREFIX + "/edit";
	}
	
	/**
	 * 修改
	 */
	@ResponseBody
	@Log("更新操作元数据")
	@PostMapping("/doEdit")
	@RequiresPermissions("md:operation:edit")
	public R doEdit(OperationVO operationVO) {
		try {
			operationService.doEdit(operationVO);
		} catch (Exception e) {
			logger.error("任务更新失败",e);
		}
		return R.ok();
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Long id) {
		OperationDO taskScheduleJob = operationService.getById(id);
		return R.ok().put("taskScheduleJob", taskScheduleJob);
	}

	/**
	 * 删除
	 */
	@Log("删除操作元数据")
	@PostMapping("/remove")
	@RequiresPermissions("md:operation:remove")
	@ResponseBody
	public R remove(Long id) {
		try {
			if (operationService.remove(id) > 0) {
				return R.ok();
			}
		} catch (Exception e) {
			logger.error("任务删除失败",e);
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@Log("批量删除操作元数据")
	@PostMapping("/batchRemove")
	@RequiresPermissions("md:operation:batchRemove")
	@ResponseBody
	public R batchRemove(@RequestParam("ids[]") Long[] ids) {
		try {
			operationService.batchRemove(ids);
		} catch (Exception e) {
			logger.error("任务批量删除失败",e);
		}

		return R.ok();
	}

	@PostMapping(value = "/changeStatus")
	@RequiresPermissions("md:operation:changeStatus")
	@ResponseBody
	public R changeStatus(Long id,String cmd ) {
		String label = "停止";
		if ("start".equals(cmd)) {
			label = "启动";
		} else {
			label = "停止";
		}
		try {
			operationService.changeStatus(id, cmd);
			return R.ok("任务" + label + "成功");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.ok("任务" + label + "失败");
	}
	
	@GetMapping("/taskCheckTree")
	String deptCheckTree() {
		return PREFIX + "/taskCheckTree";
	}
	
	@PostMapping(value = "/doExecute")
	@RequiresPermissions("md:operation:run")
	@ResponseBody
	public R doExecute(Long id) {
		try {
			operationService.runOperationJob(id);
			return R.ok("任务已转入后台执行");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return R.ok("任务执行失败");
	}

}
